package cn.com.inossem.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.format.TitleFormatter;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.inossem.base.RequestActivity;
import cn.com.inossem.model.HourApplyBean;
import cn.com.inossem.utils.MySelectorDecorator;
import cn.com.inossem.utils.TimeUtil;
import cn.com.inossem.utils.ToastUtil;

/**
 * 添加工时选择日期
 * Created by Mickey.Li on 2017-8-8.
 */

public class ProjectAddHourActivity extends RequestActivity {
	@BindView(R.id.common_title)
	TextView commonTitle;
	@BindView(R.id.calendarView)
	MaterialCalendarView widget;
	private String[] weekStrs;
	private Intent mIntent;
	private ArrayList<HourApplyBean.HourItemBean> timeList;

	@Override
	public int getContentViewId() {
		return R.layout.act_hour_choice_date;
	}

	@Override
	public void initAllMembers(Bundle savedInstanceState) {
		super.initAllMembers(savedInstanceState);
		mIntent = getIntent();
		timeList = mIntent.getParcelableArrayListExtra("hour_list");
		weekStrs = getResources().getStringArray(R.array.hour_date_week);
		commonTitle.setText("选择日期");
		findViewById(R.id.common_right_img).setVisibility(View.GONE);
		widget.setShowOtherDates(MaterialCalendarView.SHOW_ALL);
		widget.setWeekDayLabels(weekStrs);
		widget.setSelectionMode(MaterialCalendarView.SELECTION_MODE_MULTIPLE);
		widget.addDecorator(new MySelectorDecorator(this));
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.YEAR, -1);
		widget.state().edit().setMinimumDate(calendar).commit();
		calendar.add(Calendar.YEAR, +1);
		calendar.add(Calendar.DATE, 10);
		widget.state().edit().setMaximumDate(calendar).commit();
		widget.setTitleFormatter(new TitleFormatter() {
			@Override
			public CharSequence format(CalendarDay day) {
				return day.getYear() + "年" + (day.getMonth() + 1) + "月";
			}
		});
		try {
			Field topBarField = MaterialCalendarView.class.getDeclaredField("topbar");
			topBarField.setAccessible(true);
			LinearLayout topBar = (LinearLayout) topBarField.get(widget);
			topBar.setBackground(new ColorDrawable(Color.WHITE));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@OnClick({R.id.common_back, R.id.sure_addHour_btn})
	public void onViewClicked(View view) {
		switch (view.getId()) {
			case R.id.common_back:
				finish();
				break;
			case R.id.sure_addHour_btn:
				if (widget.getSelectedDates().size() == 0) {
					ToastUtil.show(this, "您未选择日期");
					return;
				}
				Set<HourApplyBean.HourItemBean> ts = new TreeSet<>(new Comparator<HourApplyBean.HourItemBean>() {
					@Override
					public int compare(HourApplyBean.HourItemBean o1, HourApplyBean.HourItemBean o2) {
						return o1.datStr.compareTo(o2.datStr);
					}
				});
				String s = new Gson().toJson(timeList);
				for (CalendarDay day : widget.getSelectedDates()) {
					if (s.contains(day.getYear() + "." + day.getMonth() + "." + day.getDay())) {
						ToastUtil.show(ProjectAddHourActivity.this, "日期选择重复,已过滤");
					}
					HourApplyBean.HourItemBean bean = new HourApplyBean.HourItemBean();
					bean.id = "";
					bean.datStr = TimeUtil.getDateStr(day.getYear() + "-" + (day.getMonth() + 1) + "-" + day.getDay());
					bean.hours = 8;
					bean.dat = TimeUtil.getDate(bean.datStr);
					timeList.add(bean);
				}
				ts.addAll(timeList);
				timeList = new ArrayList<>(ts);
				mIntent.putParcelableArrayListExtra("choice_hour", timeList);
				setResult(RESULT_OK, mIntent);
				finish();
				break;
		}
	}
}